package {
	import Classes.CLoader;
	
	import UIFree.controls.uif_input;
	import UIFree.controls.uif_label;
	import UIFree.uif_Timer;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import UIUtils.uif_util;
	
	import api_servers.pi_servers;
	
	import cardsbody.pi_pngcard;
	
	import components.pj_button;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.external.ExternalInterface;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	
	[SWF(width="760", height="400", backgroundColor="#FFFFFF")]
	public class pj_report extends mail_pj {
		private var _layer: uif_ecomponent;
		private var _image: Bitmap;
		private var _price: Object = {small: 20, big: 35};
		private var _stopCheck: Boolean;
		private var _errors: uif_label;
		private var _state: uif_input;
		
		public function pj_report() {
			photo_inject.APPHEIGHT = 400;
			
			super();
//			var report1: Object = calcTmpl(pi_data.maskInfoList, 7460, 7995, {small: 25, big: 25});

//Алена, заработано за 29-31.10.2011			var report1: Object = calcTmpl(pi_data.maskInfoList, 10595, 10682, {small: 15, big: 15});
//Алена, заработано за 01-26.11.2011			var report1: Object = calcTmpl(pi_data.maskInfoList, 10683, 11014, {small: 18, big: 18});
//Алена, заработано за 01.11-03.12.2011			var report1: Object = calcTmpl(pi_data.maskInfoList, 10683, 11116, {small: 18, big: 25});
//Алена, заработано за 05.12-28.12.2011			var report1: Object = calcTmpl(pi_data.maskInfoList, 11118, 11472, {small: 18, big: 25});
//Алена, заработано за 28.12-26.01.2012			var report1: Object = calcTmpl(pi_data.maskInfoList, 11473, 11844, {small: 18, big: 25});

//Алена, заработано за 27.01-31.01.2012			var report1: Object = calcTmpl(pi_data.maskInfoList, 11845, 11897, {small: 18, big: 25});

			var report1: Object = calcTmpl(pi_data.maskInfoList, 11898, 12296, {small: 18, big: 25, pr: 30});
/*			report1.present = (625 * 3);
			report1.sum += report1.present;*/
			
			var inner: Rectangle = getInner();
			_layer = new uif_ecomponent(layers[0], inner.x, inner.y, inner.width, inner.height, 'tabBlock');
			
			
			inner = _layer.getInner();
			var _label: uif_label = new uif_label(_layer, inner.x, inner.y, 300, 20, 'box_label');
			_label.text = 'Алена, заработано за 01.02-29.02.2012';

			createBlock(35, report1);
			
			_label = new uif_label(_layer, inner.x, inner.y + inner.height - 30, 300, 20, 'countLabelL');
			_label.text = 'Последний номер: ' + report1.lastIndex;
			
			ExternalInterface.call('setStartID', report1.lastIndex + 1);
			
/*			new pj_button(_layer, inner.x + 320, inner.y + 40, 150, 'Проверка шаблонов', function(e: Event): void {
				alert(pi_data.WARNING, 'Будут проверено наличие всех шаблонов, это займет значительное время и трафик. Вы действительно хотите запустить проверку?', doCheckTemplates, true);
			});
			
			new pj_button(_layer, inner.x + 475, inner.y + 40, 150, 'Остановить', function(e: Event): void {
				_stopCheck = true;
			});
			
			_errors = new uif_label(_layer, inner.x + 310 + 240, inner.y + 80, 195, 240, 'desc');
			
			_image = new Bitmap(new BitmapData(240, 240, false, 0xFFFFFFFF));
			_image.x = inner.x + 310;
			_image.y = inner.y + 80;
			_layer.addChild(_image);
			
			_state = new uif_input(_layer, inner.x + 635, inner.y + 45, 100, 20, 'minput');
			_state.text = '2340';
			
			*/
			dispatchEvent(new GDataEvent(GDataEvent.RESPONSE,  {
				queryID:'startAPP',
				response: {}
			})); // Обозначаем конец начала работы
		}
//17240 7140 		
		
		protected function createBlock(ay: Number, report: Object): void {
			var inner: Rectangle = _layer.getInner();
			var _label: uif_label = new uif_label(_layer, inner.x, ay, 300, 20, 'countLabelL');
			_label.text = 'Одинарные шаблоны: ' + report.smallCount + 'шт. *' + report.prices.small + ' руб. =' + report.smallSum + ' руб.';
			ay += 25;
			_label = new uif_label(_layer, inner.x, ay, 300, 20, 'countLabelL');
			_label.text = 'Двойные шаблоны: ' + report.bigCount + 'шт. *' + report.prices.big + ' руб. =' + report.bigSum + ' руб.';
			ay += 25;
			_label = new uif_label(_layer, inner.x, ay, 300, 20, 'countLabelL');
			_label.text = 'Праздничные: ' + report.prCount + 'шт. *' + report.prices.pr + ' руб. =' + report.prSum + ' руб.';
			ay += 25;
			if (report.present) {
				_label = new uif_label(_layer, inner.x, ay, 300, 20, 'countLabelL');
				_label.text = 'Премия: ' + report.present + ' руб.';
				ay += 25;
			}
			_label = new uif_label(_layer, inner.x, ay, 300, 20, 'countLabelL');
			_label.text = '';
		}
		
		override public function initLayers(count: int): void {
			_layers = new Array();
			for (var i: int = 0; i<count; i++)
				_layers.push(new uif_component(this, 0, 0, APPWIDTH, APPHEIGHT));
		}
		
		protected function doCheckTemplates(): void {
			var index: int = parseInt(_state.text);
			var loader: CLoader;
			_stopCheck = false;
			
			function loadCurrent(): void {
				if (!_stopCheck && (index < pi_data.maskInfoList.length)) {
					var tmplID: int = pi_data.maskInfoList[index].id;
					_state.text = index.toString();
					var url: String = pi_servers.MROOT_URL + 'JPG/' + tmplID + '.jpg';
					loader.load(new URLRequest(url));
				}
			}
			
			function loadNext(): void {
				if (!_stopCheck) {
					index++;
					while ((index < pi_data.maskInfoList.length) && (pi_data.maskInfoList[index].maskClass != pi_pngcard)) index++;
					new uif_Timer(300, null, function(e: Event): void {
						loadCurrent();
					}, 1);
				}
			}
			
			loader = new CLoader(_layer, '', function(e: Event): void {
				var rect: Rectangle = uif_util.EnterHere(_image.getBounds(_image), new Point(loader.contentLoaderInfo.width, loader.contentLoaderInfo.height));
				var mat: Matrix = new Matrix();
				mat.scale(rect.width / loader.contentLoaderInfo.width, rect.height / loader.contentLoaderInfo.height);
				mat.translate(rect.x, rect.y);
				_image.bitmapData.fillRect(_image.bitmapData.rect, 0xFFFFFFFF);
				_image.bitmapData.draw(loader, mat);
				loadNext();
			});
			loader.visible = false;
			
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(e: IOErrorEvent): void {
				_errors.appendText(pi_data.maskInfoList[index].id + ' ошибка!\n');
				loadNext();
			});
			
			loadNext();
		}
		
		protected function addReport(report1: Object, report2: Object): Object {
			var result: Object = {lastIndex: report2.lastIndex, prices: report2.prices};
			
			result.smallCount = report1.smallCount + report2.smallCount;
			result.bigCount	= report1.bigCount + report2.bigCount;
			
			result.smallSum = report1.smallSum + report2.smallSum;
			result.bigSum = report1.bigSum + report2.bigSum;
			result.sum = report1.sum + report2.sum;
			return result;
		}
		
		protected function calcTmpl(list: Array, startIndex: int, endIndex: int, a_price: Object): Object {
			var result: Object = {smallCount: 0, bigCount: 0, prCount: 0, lastIndex: 0, prices: a_price};
			for (var i: int=0; i<list.length; i++) {
				if ((list[i].group < 100) && (list[i].id >= startIndex) && (list[i].id <= endIndex)) {
					if (list[i].spots) result.bigCount++;
					else if ((list[i].jpg & 0x100) > 0) result.prCount++;
					else result.smallCount++;
					if (result.lastIndex < list[i].id) result.lastIndex = list[i].id;
				} 
			}
			result.smallSum = result.smallCount * a_price.small;
			result.bigSum = result.bigCount * a_price.big;
			result.prSum = result.prCount * a_price.pr;
			result.sum = result.smallSum + result.bigSum + result.prSum;
			return result;
		}  
	}
}